//
//  WatchingListViewController.m
//  Rockval
//
//  Created by TuanNT on 5/4/14.
//  Copyright (c) 2014 TNL. All rights reserved.
//

#import "WatchingListViewController.h"
#import "BuildingTableViewCell.h"
#import "DetailViewController.h"

@interface WatchingListViewController ()
@property (nonatomic, strong) NSArray *saleListing;
@property (nonatomic, strong) NSArray *leaseListing;
@end

@implementation WatchingListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
		// Custom initialization
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	[self setUpView];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.navigationController setToolbarHidden:YES animated:NO];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	// Get the new view controller using [segue destinationViewController].
	// Pass the selected object to the new view controller.
	if ([segue.identifier isEqualToString:@"BuildingDetailSegue"]) {
		BuildingTableViewCell *cell = (BuildingTableViewCell *)sender;
		NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center];
		// Get object at indexpath and fill data to detail

		DetailViewController *vc = (DetailViewController *)[segue destinationViewController];
		if (indexPath.section == 0) {
			vc.building = [self saleBuildingAtIndexPath:indexPath];
		}
		else {
			vc.building = [self leaseBuildingAtIndexPath:indexPath];
		}
	}
}

//================================ METHODS ================================
#pragma mark -
#pragma mark METHODS

- (void)setUpView {
	[self getSaleData];
	[self getLeaseData];
}

- (void)getSaleData {
	// Get watching list for sale data
	[[DataManager sharedInstance] watchingListForSaleWithBlock: ^(NSArray *list) {
	    // Update UI
	    _saleListing = [NSArray arrayWithArray:list];
	    [self.tableView reloadData];
	}];
}

- (void)getLeaseData {
	// Get watching list for sale data
	[[DataManager sharedInstance] watchingListForLeaseWithBlock: ^(NSArray *list) {
	    // Update UI
	    _leaseListing = [NSArray arrayWithArray:list];
	    [self.tableView reloadData];
	}];
}

- (Building *)saleBuildingAtIndexPath:(NSIndexPath *)indexPath {
	return self.saleListing[indexPath.row];
}

- (Building *)leaseBuildingAtIndexPath:(NSIndexPath *)indexPath {
	return self.leaseListing[indexPath.row];
}

//================================ UITABLEVIEW ================================
#pragma mark -
#pragma mark UITABLEVIEW

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	NSString *titleString = @"";
	switch (section) {
		case 0:
			titleString = @"For Sale";
			break;

		case 1:
			titleString = @"For Lease";
			break;

		default:
			break;
	}
	return titleString;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger count = 1;
	switch (section) {
		case 0:
			if (_saleListing.count > 1) {
				count = _saleListing.count;
			}
			break;

		case 1:
			if (_leaseListing.count > 1) {
				count = _leaseListing.count;
			}
			break;

		default:
			break;
	}
	return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = nil;
	switch (indexPath.section) {
		case 0: {
			if (self.saleListing.count == 0) {
				cell = [tableView dequeueReusableCellWithIdentifier:@"EmptyTableViewCell"];
			}
			else {
				cell = [tableView dequeueReusableCellWithIdentifier:@"BuildingTableViewCell"];
				BuildingTableViewCell *buildingCell = (BuildingTableViewCell *)cell;
				Building *_bd = [self saleBuildingAtIndexPath:indexPath];
				[buildingCell.buildingName setText:_bd.name];
				[buildingCell.price setText:_bd.price];
				[buildingCell.size setText:_bd.size];
				[buildingCell.type setText:_bd.main];
				[buildingCell.thumbnailImageView setImageWithURL:[NSURL URLWithString:[_bd.image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
				                     usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
			}
		}
		break;

		case 1: {
			if (self.leaseListing.count == 0) {
				cell = [tableView dequeueReusableCellWithIdentifier:@"EmptyTableViewCell"];
			}
			else {
				cell = [tableView dequeueReusableCellWithIdentifier:@"BuildingTableViewCell"];
				BuildingTableViewCell *buildingCell = (BuildingTableViewCell *)cell;
				Building *_bd = [self leaseBuildingAtIndexPath:indexPath];
				[buildingCell.buildingName setText:_bd.name];
				[buildingCell.price setText:_bd.price];
				[buildingCell.size setText:_bd.size];
				[buildingCell.type setText:_bd.main];
				[buildingCell.thumbnailImageView setImageWithURL:[NSURL URLWithString:[_bd.image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
				                     usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
			}
		}
		break;

		default:
			break;
	}
	return cell;
}

@end
